{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
define variable v_maillist as character no-undo.
define variable mailtitle as character.
define variable tocodepage as character.
define variable sendmail as character.
define variable mailattachment as character.
define variable reccnt as int.
define variable expiredays as int.

reccnt = 0.


sendmail = "/home/mfg/sendmailtopurchase.sh".
mailattachment = "/home/mfg/ExpiredPurchasePriceList.csv".

find first code_mstr where code_domain = global_domain
                       and code_fldname = "xx_email_config"
                       and code_value = "codepage"
                       no-lock no-error.
     assign tocodepage = code_cmmt.

find first code_mstr where code_domain = global_domain
                       and code_fldname = "xx_email_config"
                       and code_value = "expiredays"
                       no-lock no-error.
     assign expiredays = int(code_cmmt).

find first code_mstr where code_domain = global_domain
                      and code_fldname = "xx_email_config"
                      and code_value = "purchasetitle "
                      no-lock no-error.


    assign mailtitle = code_cmmt.

for each code_mstr where code_domain = global_domain
                 and code_fldname = "xx_email_purchase"
                 no-lock.

   v_maillist = if v_maillist = "" then  code_value
                else v_maillist + ","  +  code_value .
                 
end.



output to value(mailattachment)  convert target "CP936".   


    put unformatted getTermLabel("PRICE_LIST_BROWSE",6) "," 
                  getTermLabel("ITEM_NUMBER",8) ","
                  getTermLabel("ITEM_NUMBER",4) + getTermLabel("DESCRIPTION",9) ","
                    getTermLabel("um",8) ","
                    getTermLabel("BEGIN_DATE",8) ","
                    getTermLabel("CLOSE_DATE",8) ","
                    getTermLabel("price",8) +  getTermLabel("type",8) ","
                    getTermLabel("price",8) ","
                   
                    
                    skip. 

for each pc_mstr no-lock where pc_domain = global_domain
                           and pc_start <= today
                           and pc_expire >= today 
                           and pc_expire - expiredays <= today  
                           and pc_list_classification = 1
                           .
       
    reccnt = reccnt + 1.
    
    find pt_mstr where pt_domain = global_domain 
                   and pt_part = pc_part
                   no-lock no-error.
                   
                

    put unformatted pc_list "," 
                    pc_part     "," 
                    pt_desc1      "," 
                    pc_um "," 
                    pc_start    "," 
                    pc_expire    "," 
                    pc_amt_type    ","
                    pc_amt[1]     skip.                    
end.

output close.

if reccnt > 0 then 
do:

output to value(sendmail)  convert target tocodepage.

put unformatted "/home/mfg/App-swaks-20190914.0/swaks --body " + quoter( mailtitle) +
                " --attach " + mailattachment +
                " --tls --from qad@faway.com --header " + quoter( "Subject:" + mailtitle) + 
                " --server smtp.partner.outlook.cn --port 587 " + 
                "--auth-user qad@faway.com --auth-password Faway@2024 --to ".

put unformatted v_maillist  skip.


output close.


    unix silent value("chmod 766 " + sendmail).
    unix silent value(sendmail).
    unix silent value("mv " + sendmail + " " + sendmail + ".bak").
    unix silent value("mv " + mailattachment + " " + mailattachment + ".bak").
    
end.  